/**
 * 
 */
package cn.nawang.ebeim.ui.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;

import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.client.util.CommonMethodsUtils;

/**
 * @ClassName: FileOprt
 * @Description: 文件读写
 * @author zhangjf
 * @date 2015年10月15日 上午8:57:34
 * @version v1.0
 */
public class FileOprt {
  
  public static void main(String[] args) {
//    String filename = "F:\\test.txt";
//    System.out.println(readFile(filename)+":"+readFile(filename).equals(""));
////    writeFileCover(filename,"哈哈ddd123");
//    javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView(); 
//    File f = fsv.getHomeDirectory();     //这便是读取桌面路径的方法了  
//    System.out.println(f);
//    long t1 = System.currentTimeMillis();
//    System.out.println(datasourceSize("C:"));
//    System.out.println((System.currentTimeMillis() - t1)/1000);
    String data = ApiUtil.findFirstPageTotalByUserId("14");
    System.out.println(data);
    JSONObject json = JSONObject.parseObject(data);
    JSONObject statics = json.getJSONObject("statics");
    String availableSpaceSize = statics.getString("availableSpaceSize");
    System.out.println(availableSpaceSize);
  }
  
  public static final String PUSH_MESSAGE_FILENAME = "push_message.record";
  
  public static String readFile(String filename) {
    String fileContent = "";
    InputStreamReader read = null;
    try {
      File f = new File(filename);
      if (!f.exists()) {
        f.createNewFile();
      }
      read = new InputStreamReader(new FileInputStream(f), "UTF-8");
      BufferedReader reader = new BufferedReader(read);
      String line;
      while ((line = reader.readLine()) != null) {
        fileContent += line;
      }
      read.close();
    } catch (Exception e) {
      System.out.println("读取文件内容操作出错");
      e.printStackTrace();
    }
    return fileContent;
  }

  public static void writeFileAppend(String filename,String content){
    try {
      File file = new File(filename);
      if (!file.exists()) {
        file.createNewFile();
      }
      FileOutputStream out = new FileOutputStream(file, true);
      StringBuffer sb = new StringBuffer();
      sb.append(content);
      out.write(sb.toString().getBytes("utf-8"));
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void writeFileCover(String filename,String content) {
    PrintStream p = null;
    FileOutputStream out = null;
    try {
      out = new FileOutputStream(filename);
      p = new PrintStream(out);
      p.print(content);
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }finally{
      p.close();
      try {
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  
  public static Long datasourceSize(String root){
    long sum = 0;
    File file = new File(root);
    if(file.isDirectory()){
      File[] files = file.listFiles();
      if(files!=null){
        for (int i = 0; i < files.length; i++) {
          File child = files[i];
          sum+=datasourceSize(child.getAbsolutePath());
        }
      }
    }else{
      sum+=file.length();
    }
    return sum;
  }
  
  public static JSONObject checkDsSize(String userId,Long dsSize){
    JSONObject rs = new JSONObject();
    rs.put("code", "200");
    
    try {
      String data = ApiUtil.findFirstPageTotalByUserId(userId);
      JSONObject json = JSONObject.parseObject(data);
      JSONObject statics = json.getJSONObject("statics");
      Integer availableSpaceSize = Integer.valueOf(statics.getString("availableSpaceSize"));
      
      int dsSizeG = (int) Math.ceil(dsSize/1024.0/1024.0/1024.0);
      if((availableSpaceSize - dsSizeG)<=0){
        rs.put("code", "500");
        rs.put("message", "空间不足");
      }
    } catch (Exception e) {
      rs.put("code", "404");
      rs.put("message", "获取存储空间信息API出错！");
      e.printStackTrace();
    }
    return rs;
  }
  
}
